<?php

namespace App\Kuafu\Robot\Controller\Feishu;

use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\Base\Enum\BaseCodeEnum;
use App\Kuafu\ProductMenu\Service\ProductMenuService;
use App\Kuafu\Robot\Enum\RmsRobotCodeEnum;
use App\Kuafu\Robot\Service\RmsBotCommandService;
use App\Kuafu\Robot\Service\RmsBotSendService;
use App\Kuafu\Robot\Service\RmsBotService;
use App\Library\FeiShu\SendFs;
use Illuminate\Http\Request;

class RmsController extends BaseController
{
    public function orderStatistical(Request $request)
    {
//        $type = $request->get('type', '');
//
//        if ($type == 'url_verification') {
//            return $this->json(['challenge' => $request->get('challenge')]);
//        }
        // 根据事件 触发不同的类型
        $header = $request->get('header');
        $event  = $request->get('event');

        $eventType = $header['event_type'] ?? '';
        $message   = $event['message'] ?? [];
        $sender    = $event['sender'] ?? [];

//        SendFs::newRobot()->setTitleContent('测试', json_encode($request->all()))->send();

        // TODO 校验事件唯一性

        switch ($eventType) {
            // 单,群聊消息
            case RmsRobotCodeEnum::EVENT_TYPE_RECEIVE_V1:
                list($commandStr, $params) = RmsBotService::handleMsg($message['content']);
                $commandServer = new RmsBotCommandService($commandStr, $params, $event);
                $commandServer->dealCommand();
                break;
            default:
                // 不支持的消息
        }

//        SendFs::newRobot()->setTitleContent('测试', json_encode($request->all()))->send();
    }

    public function commandList()
    {

    }

    public function sendChatMessage(Request $request): \Illuminate\Http\JsonResponse
    {
        $message   = (string)$request->get('message');
        $receiveId = (string)$request->get('receiveId');

        $msgBot = new RmsBotSendService();
        $msgBot->setGeneralMsg($message);
        $data = $msgBot->sendMessage($receiveId, 'chat_id');

        return $this->success($data);
    }
}
